<?php
/** 
 * 后台管理员控制器
 * @DateTime 2017-06-27
 * @author zyl
 */
class AdminUser extends Admin_Controller{

	function __construct(){
		parent::__construct();
		// $class=$this->router->class;
		// $result = $this->auth_check($class);
		//加载管理员模型
		$this->load->model('Admin_model',"admin");
	}

	/**
	 * 管理员列表
	 * @DateTime 2017-06-27
	 * @author zyl
	 * @return   [type]     [description]
	 */
	function index(){
		if (!$this->authcheck('admin','select')) {
			$this->error('您无权限访问该模块');
			return;
		}
		if($_POST){
			// print_r($_POST);exit;
			$post = $this->input->post();
			$where = array(
					'status'      => $post['status'],
					'create_time>' => $post['start_time'],
					'create_time<' => $post['end_time'],
					'role' => $post['role'],
				);
			$admin = $this->admin->getAdminList($where);
		}else{
			$where = array(
					'status>=' => 0,
					'role!=' => '4',
				);
			$admin = $this->admin->getAdminList($where);
		}

		
		$admin = $this->list_to_map($admin,$this->admin->_config_);
		$data['adminuser'] = $admin;
		// print_r($admin);exit;
		$this->load->view('Setting/adminUser',$data);
		// $this->load->view('Admin/AdminUser/index',$data);
	}

	/**
	 * 顾问列表
	 * @DateTime 2017-07-07
	 * @author zyl
	 * @return   [type]     [description]
	 */
	function adviserList(){
		$where = array(
				'status' =>  1,
				'role'   => '4',
			);
		$list = $this->admin->getAdminList($where);
		$list = $this->list_to_map($list,$this->admin->_config_);
		$data['adviser'] = $list;
		// print_r($list);exit;
		$this->load->view('Consultant/consultantList',$data);
	}

	/**
	 * 添加/编辑顾问
	 * @DateTime 2017-07-07
	 * @author zyl
	 * @param    integer    $adviser_id [description]
	 */
	function addAdviser($adviser_id = 0){
		if($_POST){
			$post = $this->input->post();
			// print_r($post);exit;
			$result = $this->admin->handleAddAdmin($post);
			$this->handleResult($result);
		}else{
			$data = array();
			if($adviser_id!=0){
				$adviser = $this->admin->getAdminById($adviser_id);
				// $user['authority'] = explode(',',$user['authority']);
				$data['adviser'] = $adviser;
			}
			$this->load->view('Consultant/addConsultant',$data);
		}
	}
	/**
	 * 添加/编辑管理员
	 * @DateTime 2017-06-28
	 * @author zyl
	 */

	function addAdminUser($user_id = 0){
		
		if($_POST){
			if (!$this->authcheck('admin','add')) {
				$this->error('您无权限访问该模块');
				return;
			}
			$post = $this->input->post();
			// print_r($post);exit;
			$result = $this->admin->handleAddAdmin($post);
			$this->handleResult($result);
		}else{
			$data = array();
			if($user_id!=0){
				$user = $this->admin->getAdminById($user_id);
				// $user['authority'] = explode(',',$user['authority']);
				$data['adminuser'] = $user;
			}
			$this->load->model('AuthManager/Auth_model','auth');
			$where = array(
					'status' => 1,
					'name!='  => '婚礼顾问',
				);
			$auth = $this->auth->getAuthList($where);
			$data['auth'] = $auth;
			// print_r($data);exit();
			$this->load->view('Setting/addAdminUser',$data);
		}

	}

	/**
	 * 删除管理员
	 * @DateTime 2017-07-13
	 * @author zyl
	 * @return   [type]     [description]
	 */
	function delAdmin(){
		if (!$this->authcheck('admin','del')) {
			$this->error('您无权限访问该模块');
			return;
		}
		$post = $this->input->post();

		$data['status'] = -1;
		$where = $post['ids'];

		$return = $this->admin->updateAdmin_in($data,$where,0);

		$this->handleResult($return);
	}
	/**
	 * 冻结理员
	 * @DateTime 2017-07-13
	 * @author zyl
	 * @return   [type]     [description]
	 */
	function freeze(){
		if (!$this->authcheck('admin','add')) {
			$this->error('您无权限访问该模块');
			return;
		}
		$post = $this->input->post();

		$data['status'] = 0;
		$where = $post['ids'];

		$return = $this->admin->updateAdmin_in($data,$where);

		$this->handleResult($return);
	}
	/**
	 * 解冻商理员
	 * @DateTime 2017-07-13
	 * @author zyl
	 * @return   [type]     [description]
	 */
	function fraw(){
		if (!$this->authcheck('admin','add')) {
			$this->error('您无权限访问该模块');
			return;
		}
		$post = $this->input->post();

		$data['status'] = 1;
		$where = $post['ids'];

		$return = $this->admin->updateAdmin_in($data,$where);

		$this->handleResult($return);
	}

	function ajaxGetAdviserList(){
		$where = array(
				'status' =>  1,
				'role'   => '4',
			);
		$list = $this->admin->getAddressList($where,$_GET['val']);
		echo(json_encode($list));
	}
}